package com.sat.client.fragment

import android.os.Bundle
import com.sat.client.R
import com.sat.client.databinding.FragmentScreenBinding
import com.sat.client.utils.WebSocketManager
import com.sat.common.base.BaseFragment
import com.sat.common.base.NoViewModel

/**
 * A simple [Fragment] subclass.
 * Use the [DeviceMainPageFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class DeviceMainPageFragment : BaseFragment<NoViewModel, FragmentScreenBinding>() {

    private var videoPlayer: VideoPlayer? = null

    private var webSocketManager: WebSocketManager? = null


    override fun layoutId(): Int {

        return R.layout.fragment_screen
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        mBinding?.run {
            videoPlayer = VideoPlayer(textureView)
            webSocketManager = WebSocketManager(requireContext(), "ws://172.16.20.196:8089",
                onMessageBytes = {
                    videoPlayer?.feedData(
                        it.toByteArray()
                    )
                })
            webSocketManager?.connect()
        }

    }


    override fun onDestroyView() {
        super.onDestroyView()
        webSocketManager?.disconnect()
        webSocketManager = null
        videoPlayer = null
    }

}